package com.weizhi.server.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weizhi.server.dto.TeacherDto;
import com.weizhi.server.entity.Teacher;
import com.weizhi.server.mapper.TeacherMapper;
import com.weizhi.server.mapper.TeacherMapper;
import com.weizhi.server.service.TeacherService;
import com.weizhi.server.service.TeacherService;
import com.weizhi.server.util.PropertyCopyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    private final static Logger logger = LoggerFactory.getLogger(TeacherServiceImpl.class);

    @Resource
    private TeacherMapper teacherMapper;

    @Override
    public PageInfo list(JSONObject param) {
        PageHelper.startPage(param.getInteger("pageIndex"), param.getInteger("pageSize"));
        List<Teacher> teacherList = teacherMapper.list();
        PageInfo pageInfo = new PageInfo(teacherList);
        List<TeacherDto> teacherDtoList = PropertyCopyUtil.copyList(teacherList, TeacherDto.class);
        pageInfo.setList(teacherDtoList);
        return pageInfo;
    }

    @Override
    public List<TeacherDto> all() {
        List<Teacher> teacherList = teacherMapper.list();
        return PropertyCopyUtil.copyList(teacherList, TeacherDto.class);
    }

    @Override
    public void save(TeacherDto teacherDto) {
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherDto, teacher);
        if(teacher.getId() == null){
            teacherMapper.insertSelective(teacher);
        }else{
            teacherMapper.updateByPrimaryKey(teacher);
        }

    }

    @Override
    @Transactional
    public void delete(Long id) {
        teacherMapper.deleteByPrimaryKey(id);
    }
}
